package com.yunji.generate.util;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 *
 */
public class StringUtil {

    private static final char SEPARATOR = '_';
    private static final char SPACE = ' ';
    private static final List<String> TABLE_PREFIXES = Arrays.asList("tb_", "t_");
    private static final String SEPARATOR_BEGIN = "(";
    private static final String SEPARATOR_END = ")";
    private static final String[] SEPARATORS_A = {";", ",", " "};
    private static final String[] SEPARATORS_B = {":", "=", "-"};

    /**
     * 转驼峰法
     */
    public static String toCamelCase(String string, boolean isFirstCharacterToUpperCase) {
        if (string == null) {
            return null;
        }
        string = string.toLowerCase();
        StringBuilder sb = new StringBuilder(string.length());

        for (int index = 0; index < string.length(); index++) {
            char c = string.charAt(index);
            if (c == SEPARATOR) {
                continue;
            }
            if (sb.length() == 0) {
                sb.append(isFirstCharacterToUpperCase ? Character.toUpperCase(c) : c);
            } else {
                sb.append(string.charAt(index - 1) == SEPARATOR ? Character.toUpperCase(c) : c);
            }
        }
        return sb.toString();
    }

    public static String toBeanClassName(String tableName, List<String> tablePrefixes) {
        if (tablePrefixes != null) {
            String localTableName = tableName.toUpperCase();
            for (String s : tablePrefixes) {
                if (localTableName.startsWith(s.toUpperCase())) {
                    return toCamelCase(tableName.substring(s.length()), true);
                }
            }
        }
        return toCamelCase(tableName, true);
    }

    public static String packageNameToDirectory(String packageName) {
        if (isEmpty(packageName)) {
            return "";
        }
        return packageName.replace(".", File.separator);
    }

    /**
     * 提取简单备注
     */
    public static String truncateRemarks(String remarks) {
        if (isEmpty(remarks)) {
            return "";
        }
        // 括号拆分
        int index = remarks.indexOf(SEPARATOR_BEGIN);
        if (index >= 0) {
            return remarks.substring(0, index);
        }
        // 空格拆分
        // 0=星期一 1=星期二           不截短
        // 开始日期 0=星期一 1=星期二  截短
        String[] strings = remarks.split(" ");
        return strings.length % 2 == 0 ? remarks : strings[0].trim();
    }

    /**
     * 处理枚举表达式
     * 支持以下几种形式(举例)
     * <p>
     * 开始日期(1:星期一,2:星期日)
     * 开始日期(1:星期一;2:星期日)
     * <p>
     * 开始日期(1=星期一,2=星期日)
     * 开始日期(1=星期一;2=星期日)
     * <p>
     * 开始日期(1-星期一,2-星期日)
     * 开始日期(1-星期一;2-星期日)
     * <p>
     * 开始日期 1-星期一 2-星期日
     * 开始日期 1:星期一 2:星期日
     * <p>
     */
    public static String truncateEnumExpre(String remarks) {
        if (isEmpty(remarks)) {
            return "";
        }
        int beginIndex = remarks.indexOf(SEPARATOR_BEGIN);
        int endIndex = remarks.lastIndexOf(SEPARATOR_END);

        // 匹配到规则
        if (beginIndex >= 0 && beginIndex < endIndex) {
            String source = remarks.substring(beginIndex + 1, endIndex).trim();
            return buildExpre(source);
        }
        // 尝试使用空格拆分法
        beginIndex = remarks.indexOf(SPACE);
        String source = remarks.substring(beginIndex + 1).trim();
        return buildExpre(source);
    }

    /**
     * 组织表达式
     */
    private static String buildExpre(String source) {
        StringBuilder sb = new StringBuilder();
        List<String> pairGroups = split(source, SEPARATORS_A);
        for (String pair : pairGroups) {
            List<String> items = split(pair, SEPARATORS_B);
            if (items.size() == 2) {
                sb.append(items.get(0)).append("=").append(items.get(1)).append("&");
            }
        }
        return sb.length() == 0 ? "" : sb.deleteCharAt(sb.length() - 1).toString();
    }

    /**
     * 依据拆分符库拆分源字符
     */
    private static List<String> split(String source, String... separators) {
        for (String separator : separators) {
            String[] pairGroups = source.split(separator);
            if (pairGroups.length == 1) {
                // 跳过
                continue;
            }
            List<String> results = new ArrayList<>(pairGroups.length);
            for (String p : pairGroups) {
                results.add(p.trim());
            }
            return results;
        }
        return Collections.singletonList(source);
    }

    public static boolean isEmpty(String string) {
        return string == null || string.isEmpty();
    }

    public static void main(String[] args) {
        System.out.println(toBeanClassName("tb_aaa_bbb_ccc", TABLE_PREFIXES));
        System.out.println(toBeanClassName("t_aaa_bbb_ccc", TABLE_PREFIXES));
        System.out.println(truncateEnumExpre("状态(1=成功,2=失败)"));

        System.out.println(truncateEnumExpre("开始日期  1=星期一  2=星期日"));
        System.out.println(truncateEnumExpre("开始日期  1:星期一  2:星期日"));
    }
}